package com.project.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.project.common.Constants;

public class ConnectwithDB {
	/**
	 * 取得Connection <br/>
	 * 
	 * @return
	 * @throws NamingException
	 * @throws SQLException
	 */
	@SuppressWarnings("unused")
	public static Connection getConnection(String jndiName)
			throws NamingException, SQLException {
		if (Constants.DATA_SOURCE == 0) {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/" + jndiName);
			return ds.getConnection();
		} else {
			try {
				Class.forName("com.mysql.jdbc.Driver");
				String url = "jdbc:mysql://127.0.0.1:3306/pudu";
				String userName = "root";
				String password = "123456";
				return DriverManager.getConnection(url, userName, password);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
